home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / patch20g.lha / patch-2.0.12g11 / backupfile.c < prev    next >
C/C++ Source or Header  |  1993-05-31  |  10KB  |  404 lines

  1. /* backupfile.c -- make Emacs style backup file names
  2.    Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.
  19.    Some algorithms adapted from GNU Emacs. */
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <sys/types.h>
  25. #include "backupfile.h"
  26. #ifdef STDC_HEADERS
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #else
  30. char *index ();
  31. char *rindex ();
  32. char *malloc ();
  33. #endif
  34.  
  35. #if defined (HAVE_UNISTD_H)
  36. #include <unistd.h>
  37. #endif
  38.  
  39. #if defined(DIRENT) || defined(_POSIX_VERSION)
  40. #include <dirent.h>
  41. #define NLENGTH(direct) (strlen((direct)->d_name))
  42. #else /* not (DIRENT or _POSIX_VERSION) */
  43. #define dirent direct
  44. #define NLENGTH(direct) ((direct)->d_namlen)
  45. #ifdef SYSNDIR
  46. #include <sys/ndir.h>
  47. #endif /* SYSNDIR */
  48. #ifdef SYSDIR
  49. #include <sys/dir.h>
  50. #endif /* SYSDIR */
  51. #ifdef NDIR
  52. #include <ndir.h>
  53. #endif /* NDIR */
  54. #endif /* DIRENT or _POSIX_VERSION */
  55.  
  56. #ifndef isascii
  57. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  58. #else
  59. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  60. #endif
  61.  
  62. #if defined (_POSIX_VERSION)
  63. /* POSIX does not require that the d_ino field be present, and some
  64.    systems do not provide it. */
  65. #define REAL_DIR_ENTRY(dp) 1
  66. #else
  67. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  68. #endif
  69.  
  70. /* Which type of backup file names are generated. */
  71. enum backup_type backup_type = none;
  72.  
  73. /* The extension added to file names to produce a simple (as opposed
  74.    to numbered) backup file name. */
  75. char *simple_backup_suffix = "~";
  76.  
  77. char *basename ();
  78. char *dirname ();
  79. static char *concat ();
  80. char *find_backup_file_name ();
  81. static char *make_version_name ();
  82. static int max_backup_version ();
  83. static int version_number ();
  84.  
  85. /* Return NAME with any leading path stripped off.  */
  86.  
  87. char *
  88. basename (name)
  89.      char *name;
  90. {
  91.   char *base;
  92.  
  93.   base = rindex (name, '/');
  94.   return base ? base + 1 : name;
  95. }
  96.  
  97. #ifndef NODIR
  98. /* Return the name of the new backup file for file FILE,
  99.    allocated with malloc.  Return 0 if out of memory.
  100.    FILE must not end with a '/' unless it is the root directory.
  101.    Do not call this function if backup_type == none. */
  102.  
  103. char *
  104. find_backup_file_name (file)
  105.      char *file;
  106. {
  107.   char *dir;
  108.   char *base_versions;
  109.   int highest_backup;
  110.  
  111.   if (backup_type == simple)
  112.     {
  113.       char *s = malloc (strlen (file) + strlen (simple_backup_suffix) + 1);
  114.       addext (s, simple_backup_suffix, '~');
  115.       return s;
  116.     }
  117.   base_versions = concat (basename (file), ".~");
  118.   if (base_versions == 0)
  119.     return 0;
  120.   dir = dirname (file);
  121.   if (dir == 0)
  122.     {
  123.       free (base_versions);
  124.       return 0;
  125.     }
  126.   highest_backup = max_backup_version (base_versions, dir);
  127.   free (base_versions);
  128.   free (dir);
  129.   if (backup_type == numbered_existing && highest_backup == 0)
  130.     return concat (file, simple_backup_suffix);
  131.   return make_version_name (file, highest_backup + 1);
  132. }
  133.  
  134. /* Return the number of the highest-numbered backup file for file
  135.    FILE in directory DIR.  If there are no numbered backups
  136.    of FILE in DIR, or an error occurs reading DIR, return 0.
  137.    FILE should already have ".~" appended to it. */
  138.  
  139. static int
  140. max_backup_version (file, dir)
  141.      char *file, *dir;
  142. {
  143.   DIR *dirp;
  144.   struct dirent *dp;
  145.   int highest_version;
  146.   int this_version;
  147.   int file_name_length;
  148.   
  149.   dirp = opendir (dir);
  150.   if (!dirp)
  151.     return 0;
  152.   
  153.   highest_version = 0;
  154.   file_name_length = strlen (file);
  155.  
  156.   while ((dp = readdir (dirp)) != 0)
  157.     {
  158.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length)
  159.     continue;
  160.       
  161.       this_version = version_number (file, dp->d_name, file_name_length);
  162.       if (this_version > highest_version)
  163.     highest_version = this_version;
  164.     }
  165.   closedir (dirp);
  166.   return highest_version;
  167. }
  168.  
  169. /* Return a string, allocated with malloc, containing
  170.    "FILE.~VERSION~".  Return 0 if out of memory. */
  171.  
  172. static char *
  173. make_version_name (file, version)
  174.      char *file;
  175.      int version;
  176. {
  177.   char *backup_name;
  178.  
  179.   backup_name = malloc (strlen (file) + 16);
  180.   if (backup_name == 0)
  181.     return 0;
  182.   sprintf (backup_name, "%s.~%d~", file, version);
  183.   return backup_name;
  184. }
  185.  
  186. /* If BACKUP is a numbered backup of BASE, return its version number;
  187.    otherwise return 0.  BASE_LENGTH is the length of BASE.
  188.    BASE should already have ".~" appended to it. */
  189.  
  190. static int
  191. version_number (base, backup, base_length)
  192.      char *base;
  193.      char *backup;
  194.      int base_length;
  195. {
  196.   int version;
  197.   char *p;
  198.   
  199.   version = 0;
  200.   if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
  201.     {
  202.       for (p = &backup[base_length]; ISDIGIT (*p); ++p)
  203.     version = version * 10 + *p - '0';
  204.       if (p[0] != '~' || p[1])
  205.     version = 0;
  206.     }
  207.   return version;
  208. }
  209.  
  210. /* Return the newly-allocated concatenation of STR1 and STR2.
  211.    If out of memory, return 0. */
  212.  
  213. static char *
  214. concat (str1, str2)
  215.      char *str1, *str2;
  216. {
  217.   char *newstr;
  218.   char str1_length = strlen (str1);
  219.  
  220.   newstr = malloc (str1_length + strlen (str2) + 1);
  221.   if (newstr == 0)
  222.     return 0;
  223.   strcpy (newstr, str1);
  224.   strcpy (newstr + str1_length, str2);
  225.   return newstr;
  226. }
  227.  
  228. /* Return the leading directories part of PATH,
  229.    allocated with malloc.  If out of memory, return 0.
  230.    Assumes that trailing slashes have already been
  231.    removed.  */
  232.  
  233. char *
  234. dirname (path)
  235.      char *path;
  236. {
  237.   char *newpath;
  238.   char *slash;
  239.   int length;    /* Length of result, not including NUL. */
  240.  
  241.   slash = rindex (path, '/');
  242.   if (slash == 0)
  243.     {
  244.       /* File is in the current directory.  */
  245.       path = ".";
  246.       length = 1;
  247.     }
  248.   else
  249.     {
  250.       /* Remove any trailing slashes from result. */
  251.       while (slash > path && *slash == '/')
  252.         --slash;
  253.  
  254.       length = slash - path + 1;
  255.     }
  256.   newpath = malloc (length + 1);
  257.   if (newpath == 0)
  258.     return 0;
  259.   strncpy (newpath, path, length);
  260.   newpath[length] = 0;
  261.   return newpath;
  262. }
  263.  
  264. /* If ARG is an unambiguous match for an element of the
  265.    null-terminated array OPTLIST, return the index in OPTLIST
  266.    of the matched element, else -1 if it does not match any element
  267.    or -2 if it is ambiguous (is a prefix of more than one element). */
  268.  
  269. int
  270. argmatch (arg, optlist)
  271.      char *arg;
  272.      char **optlist;
  273. {
  274.   int i;            /* Temporary index in OPTLIST. */
  275.   int arglen;            /* Length of ARG. */
  276.   int matchind = -1;        /* Index of first nonexact match. */
  277.   int ambiguous = 0;        /* If nonzero, multiple nonexact match(es). */
  278.   
  279.   arglen = strlen (arg);
  280.   
  281.   /* Test all elements for either exact match or abbreviated matches.  */
  282.   for (i = 0; optlist[i]; i++)
  283.     {
  284.       if (!strncmp (optlist[i], arg, arglen))
  285.     {
  286.       if (strlen (optlist[i]) == arglen)
  287.         /* Exact match found.  */
  288.         return i;
  289.       else if (matchind == -1)
  290.         /* First nonexact match found.  */
  291.         matchind = i;
  292.       else
  293.         /* Second nonexact match found.  */
  294.         ambiguous = 1;
  295.     }
  296.     }
  297.   if (ambiguous)
  298.     return -2;
  299.   else
  300.     return matchind;
  301. }
  302.  
  303. /* Error reporting for argmatch.
  304.    KIND is a description of the type of entity that was being matched.
  305.    VALUE is the invalid value that was given.
  306.    PROBLEM is the return value from argmatch. */
  307.  
  308. void
  309. invalid_arg (kind, value, problem)
  310.      char *kind;
  311.      char *value;
  312.      int problem;
  313. {
  314.   fprintf (stderr, "patch: ");
  315.   if (problem == -1)
  316.     fprintf (stderr, "invalid");
  317.   else                /* Assume -2. */
  318.     fprintf (stderr, "ambiguous");
  319.   fprintf (stderr, " %s `%s'\n", kind, value);
  320. }
  321.  
  322. static char *backup_args[] =
  323. {
  324.   "never", "simple", "nil", "existing", "t", "numbered", 0
  325. };
  326.  
  327. static enum backup_type backup_types[] =
  328. {
  329.   simple, simple, numbered_existing, numbered_existing, numbered, numbered
  330. };
  331.  
  332. /* Return the type of backup indicated by VERSION.
  333.    Unique abbreviations are accepted. */
  334.  
  335. enum backup_type
  336. get_version (version)
  337.      char *version;
  338. {
  339.   int i;
  340.  
  341.   if (version == 0 || *version == 0)
  342.     return numbered_existing;
  343.   i = argmatch (version, backup_args);
  344.   if (i >= 0)
  345.     return backup_types[i];
  346.   invalid_arg ("version control type", version, i);
  347.   exit (1);
  348. }
  349. #endif /* NODIR */
  350.  
  351. /* Append to FILENAME the extension EXT, unless the result would be too long,
  352.    in which case just append the character E.  */
  353.  
  354. void
  355. addext (filename, ext, e)
  356.      char *filename, *ext;
  357.      int e;
  358. {
  359.   char *s = rindex (filename, '/');
  360.   int extlen = strlen (ext), slen;
  361.   long slen_max = -1;
  362.  
  363.   s = s ? s + 1 : filename;
  364.   slen = strlen(s);
  365. #if HAVE_PATHCONF && defined (_PC_NAME_MAX)
  366. #ifndef _POSIX_NAME_MAX
  367. #define _POSIX_NAME_MAX 14
  368. #endif
  369.   if (slen + extlen <= _POSIX_NAME_MAX)
  370.     /* The file name is so short there's no need to call pathconf.  */
  371.     slen_max = _POSIX_NAME_MAX;
  372.   else if (s == filename)
  373.     slen_max = pathconf (".", _PC_NAME_MAX);
  374.   else
  375.     {
  376.       char c = *s;
  377.       *s = 0;
  378.       slen_max = pathconf (filename, _PC_NAME_MAX);
  379.       *s = c;
  380.     }
  381. #endif
  382.   if (slen_max == -1) {
  383. #if HAVE_LONG_FILE_NAMES
  384.     slen_max = 255;
  385. #else
  386.     slen_max = 14;
  387. #endif
  388.   }
  389.   if (slen + extlen <= slen_max)
  390.     strcpy (s + slen, ext);
  391.   else
  392.     {
  393.       if (slen_max <= slen) {
  394.     /* Try to preserve difference between .h .c etc.  */
  395.     if (slen == slen_max && s[slen - 2] == '.')
  396.       s[slen - 2] = s[slen - 1];
  397.  
  398.     slen = slen_max - 1;
  399.       }
  400.       s[slen] = e;
  401.       s[slen + 1] = 0;
  402.     }
  403. }
  404.